<?php

namespace App\Admin\Controllers;

use App\Operator;
use App\Gz;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use App\Admin\Extensions\ExcelExpoter;

class GzController extends CommonController
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('故障管理');

            $grid = Admin::grid(Gz::class, function(Grid $grid){


//                $grid->id('ID')->sortable();
                  $grid->model()->orderBy('id', 'desc');

//                $grid->operator_id('机房名称')->display(function ($operatorId){
//                    return Operator::find($operatorId)->name ;
//                });
                $grid->operator()->name("机房名称");

                $grid->operatortype()->name("机房类型");

                $grid->operatorline()->name("机房线路");

                $grid->column('begintime',"开始时间");

                $grid->column('endtime',"结束时间");

                $grid->column('reason',"原因");

                $grid->column('result',"结果");

                $grid->column('instructions',"说明");

//                $grid->status('状态')->display(function ($status) {
//                    return $status ? '是' : '否';
//                });

                $excel = new ExcelExpoter();
                $excel->setAttr(['故障id','机房名称','机房类型','机房线路','故障开始时间','故障结束时间','故障原因','后续处理结果','是否提供故障说明' ], ['id','operator.name','operatortype.name','operatorline.name','begintime','endtime','reason','result','instructions']);
                $grid->exporter($excel);

                $grid->filter(function ($filter) {
                    $operator = new Operator();
                    $getoperator = $operator->getoperators();
                    $operators = $this->filter($getoperator);
                    $filter->in('operator_id', "机房名称 ")->multipleSelect($operators);
                });


            });
            $content->body($grid);
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改故障说明');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增故障说明');
            $grid = Admin::form(Gz::class, function(Form $form){

                $operator = new Operator();
                $getoperator = $operator->getoperators();
                $operators = $this->filter($getoperator);

                $form->select('operator_id', '机房名称')->options($operators);

                $form->text('reason', '故障原因');

                $form->text('result', '处理结果');

                $form->text('instructions', '故障说明');

                $form->datetimeRange('begintime', 'endtime', '故障时间');

                // 两个时间显示
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '修改时间');
            });
            $content->body($grid);
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Gz::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Gz::class, function (Form $form) {
            $form->hidden('id');

            $form->display('id', '故障ID');

            $operator = new Operator();
            $getoperator = $operator->getoperators();
            $operators = $this->filter($getoperator);

            $form->select('operator_id', '机房名称')->options($operators);

            $form->text('reason', '故障原因');

            $form->hidden('line');

            $form->hidden('operator_type');

            $form->text('result', '处理结果');

            $form->text('instructions', '故障说明');

            $form->datetimeRange('begintime', 'endtime', '故障时间');
        });
    }

    public function store(Request $request)
    {
        $gz = new Gz();
        $gz->operator_id = $request->input('operator_id');
        $gz->begintime = $request->input('begintime');
        $gz->endtime = $request->input('endtime');
        $gz->reason = $request->input('reason');
        $gz->result = $request->input('result');
        $operator = Operator::select('line','type')->where('id',$request->operator_id)->get()->toArray();
        $gz->line = $operator[0]['line'];
        $gz->operator_type = $operator[0]['type'];
        $gz->instructions = $request->input('instructions');
        $gz->save();
    }

    public function update(Request $request)
    {
        $gz = new Gz();
        $data['operator_id'] = $request->input('operator_id');
        $data['begintime'] = $request->input('begintime');
        $data['endtime'] = $request->input('endtime');
        $data['reason'] = $request->input('reason');
        $data['result'] = $request->input('result');
        $operator = Operator::select('line','type')->where('id',$request->operator_id)->get()->toArray();
        $data['line'] = $operator[0]['line'];
        $data['operator_type'] = $operator[0]['type'];
        $data['instructions'] = $request->input('instructions');
        $gz->where('id',$request->input('id'))->update($data);
        return redirect("/admin/guzhang");
    }

}
